﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CMISLibrary.Entities;
using BCNPCObject.DB;

namespace BCNPCObject
{
    public class cls_NPC_TRTH_KHANG_Controller
    {
        #region   Atributes

        private DataSet CMIS_Header = new CMISOutputParameter();
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();
        private NPC_TRTH_KHANG info;

        #endregion

        #region   Constructor

        public cls_NPC_TRTH_KHANG_Controller()
        {
            info = new NPC_TRTH_KHANG();
        }
        public cls_NPC_TRTH_KHANG_Controller(NPC_TRTH_KHANG Info)
        {
            this.info = Info;
        }

        #endregion

        #region   Properties

        public NPC_TRTH_KHANG pInfor
        {
            get { return info; }
            set { info = value; }
        }

        #endregion

        #region Method

        public IEnumerable<NPC_TRTH_KHANG> select_NPC_TRTH_KHANG_All(string strMadviqly)
        {
            try
            {
                CMIS2.ResetDB();
                var q = from p in CMIS2.DB.NPC_TRTH_KHANG 
                        where p.MA_DVIQLY == strMadviqly 
                        orderby p.ID_TRTH 
                        select p;

                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<NPC_TRTH_KHANG> select_NPC_TRTH_KHANG_by_id_truythu(long intID)
        {
            try
            {
                CMIS2.ResetDB();
                var q = from p in CMIS2.DB.NPC_TRTH_KHANG
                        where p.ID_TRTH == intID
                        select p;
                return q;
            }
            catch
            {                
                CMIS2.ResetDB();
                return null;
            }
        }

        public IEnumerable<NPC_TRTH_KHANG> select_NPC_TRTH_KHANG_by_Ma_khang(string strMa_khang)
        {
            try
            {
                CMIS2.ResetDB();
                var q = from p in CMIS2.DB.NPC_TRTH_KHANG
                        where p.MA_KHANG == strMa_khang
                        select p;

                return q;
            }
            catch
            {
                CMIS2.ResetDB();
                return null;
            }
        }

        public IEnumerable<NPC_TRTH_KHANG> select_NPC_TRTH_KHANG_by_1_Col_co()
        {
            try
            {
                CMIS2.ResetDB();

                var q = from p in CMIS2.DB.NPC_TRTH_KHANG
                        orderby p.ID_TRTH descending
                        where p.MA_DVIQLY == info.MA_DVIQLY
                        && (info.ID_TRTH != -1 && p.ID_TRTH == info.ID_TRTH || info.ID_TRTH == -1 )
                        && (info.MA_KHANG != " " && p.MA_KHANG == info.MA_KHANG || info.MA_KHANG == " ")
                        && (!p.LOAI_KHANG.Contains("2"))
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<NPC_TRTH_KHANG> select_NPC_TRTH_KHANG_by_1_Col_dtu()
        {
            try
            {
                CMIS2.ResetDB();

                var q = from p in CMIS2.DB.NPC_TRTH_KHANG
                        orderby p.ID_TRTH descending
                        where p.MA_DVIQLY == info.MA_DVIQLY
                        && (info.ID_TRTH != -1 && p.ID_TRTH == info.ID_TRTH || info.ID_TRTH == -1)
                        && (info.MA_KHANG != " " && p.MA_KHANG == info.MA_KHANG || info.MA_KHANG == " ")
                        && (p.LOAI_KHANG.Contains("2"))
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public bool insert_NPC_TRTH_KHANG(ref string strErr)
        {
            try
            {
                NPC_TRTH_KHANG objNPC_TRTH_KHANG = new NPC_TRTH_KHANG()
                {                    
                    ID_TRTH = info.ID_TRTH ,
                    MA_DVIQLY = info.MA_DVIQLY ,
                    TEN_DDIEN1 = info.TEN_DDIEN1 ,
                    CVU_DDIEN1 = info.CVU_DDIEN1 ,
                    TEN_DDIEN2 = info.TEN_DDIEN2 ,
                    CVU_DDIEN2 = info.CVU_DDIEN2 ,
                    TEN_DDIEN3 = info.TEN_DDIEN3 ,
                    CVU_DDIEN3 = info.CVU_DDIEN3 ,
                    MA_KHANG = info.MA_KHANG ,
                    TEN_KHANG = info.TEN_KHANG ,
                    DIA_CHI = info.DIA_CHI ,
                    NGUOI_TAO = info.NGUOI_TAO,
                    NGAY_TAO = info.NGAY_TAO,
                    DDIEN_MUA = info.DDIEN_MUA, 
                    CVU_MUA = info.CVU_MUA,
                    NGUYEN_NHAN = info.NGUYEN_NHAN,
                    LOAI_KHANG = info.LOAI_KHANG,
                    DCHI_TTOAN = info.DCHI_TTOAN,
                    LOAI_CTO = info.LOAI_CTO,
                    NGAY_KDINH = info.NGAY_KDINH,
                    CAN_CU = info.CAN_CU, 
                    DDIEN_MUA2 = info.DDIEN_MUA2,
                    CVU_MUA2 = info.CVU_MUA2,
                    HS_COSFI = info.HS_COSFI,
                    KCOSFI = info.KCOSFI
                    
                };
                CMIS2.DB.NPC_TRTH_KHANG.InsertOnSubmit(objNPC_TRTH_KHANG);
                CMIS2.DB.SubmitChanges();
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                CMIS2.ResetDB(); return false;
            }
        }

        public bool update_NPC_TRTH_KHANG(ref string strErr)
        {
            try
            {
                NPC_TRTH_KHANG objNPC_TRTH_KHANG = CMIS2.DB.NPC_TRTH_KHANG.Single(c => c.ID_TRTH == info.ID_TRTH && c.MA_DVIQLY == info.MA_DVIQLY);

                objNPC_TRTH_KHANG.TEN_DDIEN1 = info.TEN_DDIEN1;
                objNPC_TRTH_KHANG.CVU_DDIEN1 = info.CVU_DDIEN1 ;
                objNPC_TRTH_KHANG.TEN_DDIEN2 = info.TEN_DDIEN2 ;
                objNPC_TRTH_KHANG.CVU_DDIEN2 = info.CVU_DDIEN2 ;
                objNPC_TRTH_KHANG.TEN_DDIEN3 = info.TEN_DDIEN3 ;
                objNPC_TRTH_KHANG.CVU_DDIEN3 = info.CVU_DDIEN3 ;
                objNPC_TRTH_KHANG.MA_KHANG = info.MA_KHANG ;
                objNPC_TRTH_KHANG.TEN_KHANG = info.TEN_KHANG ;
                objNPC_TRTH_KHANG.DIA_CHI = info.DIA_CHI;
                objNPC_TRTH_KHANG.NGUOI_TAO = info.NGUOI_TAO ;
                objNPC_TRTH_KHANG.NGAY_TAO = info.NGAY_TAO ;
                objNPC_TRTH_KHANG.DDIEN_MUA = info.DDIEN_MUA;
                objNPC_TRTH_KHANG.CVU_MUA = info.CVU_MUA;
                objNPC_TRTH_KHANG.NGUYEN_NHAN = info.NGUYEN_NHAN;
                objNPC_TRTH_KHANG.LOAI_KHANG = info.LOAI_KHANG;
                objNPC_TRTH_KHANG.DCHI_TTOAN = info.DCHI_TTOAN;
                objNPC_TRTH_KHANG.LOAI_CTO = info.LOAI_CTO;
                objNPC_TRTH_KHANG.NGAY_KDINH = info.NGAY_KDINH;
                objNPC_TRTH_KHANG.CAN_CU = info.CAN_CU;
                objNPC_TRTH_KHANG.DDIEN_MUA2 = info.DDIEN_MUA2;
                objNPC_TRTH_KHANG.CVU_MUA2 = info.CVU_MUA2;
                objNPC_TRTH_KHANG.HS_COSFI = info.HS_COSFI;
                objNPC_TRTH_KHANG.KCOSFI = info.KCOSFI;


                CMIS2.DB.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                CMIS2.ResetDB(); return false;
            }
        }

        public bool delete_NPC_TRTH_KHANG(ref string strErr)
        {
            try
            {
                var query = from p in CMIS2.DB.NPC_TRTH_KHANG where p.ID_TRTH == info.ID_TRTH select p;
                if (query != null && query.Count() > 0)
                {
                    CMIS2.DB.NPC_TRTH_KHANG.DeleteAllOnSubmit(query.ToList());
                    CMIS2.DB.SubmitChanges();
                    return true;
                }
                return true;
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                CMIS2.ResetDB(); return false;
            }
        }

        public long GetNextSEQ_NPC_TRUYTHU(ref string strErr)
        {
            try
            {
                strErr = "";

                long _ID = CMIS2.DB.ExecuteCommand("select SEQ_NPC_TRUYTHU.nextval from dual", ref strErr);

                if (strErr == "") return _ID;

                else return -1;
            }
            catch
            {
                CMIS2.ResetDB();
                return -1;
            }
        }


        #endregion
    }
}
